package org.academic.texteditor.gui.util;

import org.apache.commons.beanutils.PropertyUtils;


public class EncapsulateObject<T> {

	private final T object;
	private String[] attributes;

	public EncapsulateObject(T object) {
		this.object = object;
	}

	public T getObject() {
		return object;
	}

	public void setAtributo(String... attributes) {
		this.attributes = attributes;
	}
	
	
    public boolean equals(Object obj) {
    	if (obj instanceof EncapsulateObject) {
    		EncapsulateObject<?> e = (EncapsulateObject<?>) obj;
    		if (e.getObject().equals(object)) {
    			return true;
    		}
    	}
	    return false;
    }

	public String toString() {
		if (object == null) {
			return null;
		}
		if (attributes == null || attributes.length == 0) {
			return object.toString();
		}
		StringBuffer buf = new StringBuffer();
		for (String attribute : attributes) {
	        try {
	            buf.append(PropertyUtils.getProperty(object, attribute));
            } catch (Exception e) {
	            buf.append(attribute);
            }
            buf.append(" ");
        }
		
		return buf.toString();
	}
	
}

